﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;
using YJKCopyModel.YJKRevitWndr;

namespace YJKCopyModel.InteractiveOperation.CmdTestInteractiveOperation
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdTestGetDoorAndWindowBlockRef : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = new Transaction(commandData.Application.ActiveUIDocument.Document);
			Result result;
			try
			{
				DwgEntitiesSelection dwgEntitiesSelection = new DwgEntitiesSelection(commandData.Application.ActiveUIDocument);
				List<DoorAndWindowBlockRefInfo> list = new List<DoorAndWindowBlockRefInfo>();
				bool flag = !dwgEntitiesSelection.TryPickSameLayerBlockRef<DoorAndWindowBlockRefInfo>(out list, false, "请选择块参照") || list.IsEmpty<DoorAndWindowBlockRefInfo>();
				if (flag)
				{
					bool flag2 = dwgEntitiesSelection.Result == DwgEntitiesSelectionResult.eUnitError;
					if (flag2)
					{
						YJKMessageBox.Information("链接图纸的单位不是“毫米”，请重新链接图纸并将导入单位设置为“毫米”");
					}
					else
					{
						bool flag3 = dwgEntitiesSelection.Result == DwgEntitiesSelectionResult.eDwgPathError;
						if (flag3)
						{
							YJKMessageBox.Information("所链接图纸的源文件不存在，请重新链接。");
						}
					}
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					List<DoorAndWindowBlockRefInfo> lstBlockRefInfo = new List<DoorAndWindowBlockRefInfo>();
					bool flag4 = !dwgEntitiesSelection.TryGetEntitesInPickBox<DoorAndWindowBlockRefInfo>(out lstBlockRefInfo, list, delegate(DoorAndWindowBlockRefInfo blockRef, PickedBox pickBox)
					{
						XYZ useLocationPoint = blockRef.UseLocationPoint;
						XYZ xyz = new XYZ(Math.Min(pickBox.Min.X, pickBox.Max.X), Math.Min(pickBox.Min.Y, pickBox.Max.Y), 0.0);
						XYZ xyz2 = new XYZ(Math.Max(pickBox.Min.X, pickBox.Max.X), Math.Max(pickBox.Min.Y, pickBox.Max.Y), 0.0);
						return useLocationPoint.X > xyz.X && useLocationPoint.Y > xyz.Y && useLocationPoint.X < xyz2.X && useLocationPoint.Y < xyz2.Y;
					}, "请框选范围");
					if (flag4)
					{
						result = Autodesk.Revit.UI.Result.Cancelled;
					}
					else
					{
						List<TextInfo> lstTextInfo = new List<TextInfo>();
						bool flag5 = !dwgEntitiesSelection.TryPickSameLayerText(out lstTextInfo, false, "请选择门窗标注文字");
						if (flag5)
						{
							bool flag6 = dwgEntitiesSelection.Result == DwgEntitiesSelectionResult.eUnitError;
							if (flag6)
							{
								YJKMessageBox.Information("链接图纸的单位不是“毫米”，请重新链接图纸并将导入单位设置为“毫米”");
							}
							else
							{
								bool flag7 = dwgEntitiesSelection.Result == DwgEntitiesSelectionResult.eDwgPathError;
								if (flag7)
								{
									YJKMessageBox.Information("所链接图纸的源文件不存在，请重新链接。");
								}
							}
							result = Autodesk.Revit.UI.Result.Cancelled;
						}
						else
						{
							Dictionary<DoorAndWindowBlockRefInfo, string> dictionary = DoorAndWindowManager.DistinguishNote(lstBlockRefInfo, lstTextInfo);
							transaction.Start("标注门窗块参照信息");
							foreach (KeyValuePair<DoorAndWindowBlockRefInfo, string> keyValuePair in dictionary)
							{
								DoorAndWindowBlockRefInfo key = keyValuePair.Key;
								string value = keyValuePair.Value;
								bool flag8 = key.BlockName.IsEmpty<char>();
								if (!flag8)
								{
									bool flag9 = value != null;
									if (flag9)
									{
										TextNote textNote = UnifiedModified.HYCreatTextNote(commandData.Application.ActiveUIDocument.Document, "宋体", value, key.UseLocationPoint);
									}
									Line curCurve = Line.CreateBound(key.UseLocationPoint, key.UseLocationPoint + key.FaceDir.Normalize() * 1000.0 / 304.8);
									TestCommon.CreateModelCurve(curCurve, commandData.Application.ActiveUIDocument.Document, new Autodesk.Revit.DB.Color(byte.MaxValue, 0, 0), 1);
									curCurve = Line.CreateBound(key.UseLocationPoint, key.UseLocationPoint + key.HandDir.Normalize() * 1000.0 / 304.8);
									TestCommon.CreateModelCurve(curCurve, commandData.Application.ActiveUIDocument.Document, new Autodesk.Revit.DB.Color(125, 0, 125), 1);
								}
							}
							transaction.Commit();
							YJKMessageBox.Information("执行成功。\n 红色线：门窗朝向；\n 紫色线：门窗位置方向；\n 交点：门窗块插入位置");
							result = Autodesk.Revit.UI.Result.Succeeded;
						}
					}
				}
			}
			catch (Exception)
			{
				transaction.RollBack();
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		private Application m_app;

		private Document m_doc;
	}
}
